package com.hq.lgx

import android.app.Activity
import android.content.Context
import android.graphics.Color.RED
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop

class MainActivity: Activity(),SensorEventListener {
    var systemService:SensorManager? = null;
    var textView:TextView ? = null;
    var sensor:Sensor? = null;
    var lanGame:LanGame? = null;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initview()
        initSensor();
        gameJoinLan()
    }


    private fun gameJoinLan(){
        lanGame?.init()
        var ip = findViewById<TextView>(R.id.ip)
       val  handler = Handler(Looper.getMainLooper(), {
           if (it.what == 1000){
               var text =    it.obj.toString()
               ip.text = "❤"
               var x = text.split(',')[0]
               var y = text.split(',')[1]
               var marginTop = ip.marginTop
               var marginLeft = ip.marginLeft
               var marginRight = ip.marginRight
               var marginBottom = ip.marginBottom
               val lp =ip.getLayoutParams() as ConstraintLayout.LayoutParams
               //方式一: 设置边距 且控制大小
               lp.setMargins(marginLeft.plus(-x.toFloat()).toInt(), marginTop.plus(y.toFloat()).toInt(), marginRight.plus(x.toFloat()).toInt(), marginBottom.plus(-y.toFloat()).toInt())
               //设置宽高
               ip.setLayoutParams(lp)

               ip.setTextColor(RED)
       }
           false
       })

        Thread{
            while (true){
               var recevie = lanGame?.receive()
               val message = Message()
                message.what=1000
                message.obj = recevie

                handler.sendMessage(message)
            }
        }.start()
    }

    private fun initSensor() {
        systemService = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        sensor = systemService!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

    }

    private fun initview() {
        textView = findViewById(R.id.textview)
        lanGame = LanGame()
    }
    //传感值变化回调
    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {

    }
    //传感器事件
    override fun onSensorChanged(event: SensorEvent?) {
            val values = event?.values
            var x = values!![0]
            var y = values!![1]

            textView?.text = "❤"
            var marginTop = textView!!.marginTop
            var marginLeft = textView!!.marginLeft
            var marginRight = textView!!.marginRight
            var marginBottom = textView!!.marginBottom
        val lp = textView?.getLayoutParams() as ConstraintLayout.LayoutParams
        //方式一: 设置边距 且控制大小
        lp.setMargins(marginLeft.plus(-x.toInt()), marginTop.plus(y.toInt()), marginRight.plus(x.toInt()), marginBottom.plus(-y.toInt()))
        //设置宽高
        textView?.setLayoutParams(lp)

        textView?.setTextColor(RED)
        var xy = ""+x+","+y+","
        Thread {
            lanGame?.sayHelloToAll(xy)
        }.start()
    }

    override fun onResume() {
        super.onResume()
//        注册传感器事件

        systemService?.registerListener(this,sensor,200)
    }

    override fun onPause() {
        super.onPause()
//        解除注册
        systemService?.unregisterListener(this)
    }
}
